package org.domeos.framework.api.controller.loadBalancer;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.domeos.basemodel.HttpResponseTemp;
import org.domeos.basemodel.ResultStat;
import org.domeos.framework.api.consolemodel.loadBalancer.LoadBalancerDraft;
import org.domeos.framework.api.controller.common.BaseSreController;
import org.domeos.framework.api.service.loadBalancer.LoadBalancerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Controller
@RequestMapping("/sre/api/loadBalancer")
@Api("SRE部署相关API")
public class LoadBalancerSreController extends BaseSreController {
	@Autowired
    LoadBalancerService lbService;
	
	@ApiOperation(value="创建负载均衡")
    @ResponseBody
    @RequestMapping(value = "/create", method = RequestMethod.POST)
	public HttpResponseTemp<?> addLoadBalancer(HttpServletRequest request,@RequestBody LoadBalancerDraft lbDraft)
            throws Exception {
		String userId = getUserId(request);
		if(StringUtils.isEmpty(userId)){
			return ResultStat.PARAM_ERROR.wrap("LoadBalancer userId is null!");
		}
		lbDraft.setUserId(getIntValue(userId));
        return lbService.sreCreateLoadBalancer(lbDraft);
    }
	
	@ApiOperation(value="删除负载均衡")
    @ResponseBody
    @RequestMapping(value = "/id/{lbId}", method = RequestMethod.DELETE)
    public HttpResponseTemp<?> removeLoadBalancer(HttpServletRequest request,@PathVariable int lbId) throws Exception {
		String userId = getUserId(request);
		if(StringUtils.isEmpty(userId)){
			return ResultStat.PARAM_ERROR.wrap("LoadBalancer userId is null!");
		}
		lbService.sreRemoveLoadBalancer(getIntValue(userId),lbId);
        return ResultStat.OK.wrap(null);
    }
	
	@ApiOperation(value="删除负载均衡")
    @ResponseBody
    @RequestMapping(value = "/name/{lbName}", method = RequestMethod.DELETE)
    public HttpResponseTemp<?> removeLoadBalancer(HttpServletRequest request,@PathVariable String lbName) throws Exception {
		String userId = getUserId(request);
		lbService.sreRemoveLoadBalancer(getIntValue(userId),lbName);
        return ResultStat.OK.wrap(null);
    }
	
	@ApiOperation(value="获取一个随机端口")
    @ResponseBody
    @RequestMapping(value = "/getRandomPort", method = RequestMethod.GET)
    public HttpResponseTemp<?> getRandomPort(HttpServletRequest request) throws Exception {
		String userId = getUserId(request);
		if(StringUtils.isEmpty(userId)){
			return ResultStat.PARAM_ERROR.wrap("LoadBalancer userId is null!");
		}
        return lbService.sreGetRandomPort(getIntValue(userId));
    }
	
	@ApiOperation(value="通过lbcid获取lb list")
    @ResponseBody
    @RequestMapping(value = "/list/{lbcId}", method = RequestMethod.GET)
    public HttpResponseTemp<?> listLoadBalancer(HttpServletRequest request,@PathVariable int lbcId) throws Exception {
		String userId = getUserId(request);
		if(StringUtils.isEmpty(userId)){
			return ResultStat.PARAM_ERROR.wrap("LoadBalancer userId is null!");
		}
        return lbService.sreListLoadBalancer(getIntValue(userId),lbcId);
    }
	
	@ApiOperation(value="获取lb")
    @ResponseBody
    @RequestMapping(value = "/id/{lbId}", method = RequestMethod.GET)
    public HttpResponseTemp<?> getLoadBalancer(HttpServletRequest request,@PathVariable int lbId) throws Exception {
		String userId = getUserId(request);
		if(StringUtils.isEmpty(userId)){
			return ResultStat.PARAM_ERROR.wrap("LoadBalancer userId is null!");
		}
        return lbService.sreGetLoadBalancer(lbId);
    }
	
	@ApiOperation(value="获取lb")
    @ResponseBody
    @RequestMapping(value = "/name/{lbName}", method = RequestMethod.GET)
    public HttpResponseTemp<?> getLoadBalancer(HttpServletRequest request,@PathVariable String lbName) throws Exception {
		String userId = getUserId(request);
		if(StringUtils.isEmpty(userId)){
			return ResultStat.PARAM_ERROR.wrap("LoadBalancer userId is null!");
		}
        return lbService.sreGetLoadBalancer(lbName);
    }

}
